import { _decorator, Component, Node } from 'cc';
import { AdComponent } from '../AdComponent';
import *as cc from "cc";
import DatasSpace from '../Tool/DatasSpace';
import Util from '../Util';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('PiggyBank')
export class PiggyBank extends Component {

  @property(Node)
  viewNode: Node = null;

  @property(cc.ProgressBar)
  progress: cc.ProgressBar = null;

  btnClose() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    this.btnCloseSetting();

  }

  start() {
    console.log('世界排行榜');
    // AdComponent.Instance.show5X5CustomAd();
    AdComponent.Instance.showInterstitialFullShow();
    AdComponent.Instance.showBannerAd();
    // tween(this.viewNode).to(0.3, { position:v3(this.viewNode.getPosition().x,-50,0), scale:v3(1.2,0.8,0)}, { easing: 'smooth' }).to(0.3, { position: v3(0,0,0), scale:v3(1,1,0) }, { easing: 'backInOut' }).to(0.3, { scale:v3(1.1,0.9,0)}, { easing: 'backInOut' }).to(0.1, { scale:v3(1,1,0) }, { easing: 'backInOut' }).call(() => {
    // }).start();\
    this.checkPiggyBankProgress();
  }

  protected onDestroy(): void {
    AdComponent.Instance.hideBannerAd();
  }

  btnCloseSetting() {
    // AdComponent.Instance.instance.playAudio(2);
    // AdComponent.Instance.hide5X5customAd();
    cc.tween(this.viewNode).to(0.5, { position: cc.v3(0, this.viewNode.getPosition().y + 1600, 0) }, { easing: 'backInOut' }).call(() => {
      this.node.removeFromParent();
      this.node.destroy();

    }).start();
  }

  /**
   * @fn 检查存钱进度
   * @time 2024 5 29 15:11
   */
  checkPiggyBankProgress() {
    this.progress.progress = Math.floor((DatasSpace.Instance.piggyBankGlodNum / 600) * 100) / 100;

  }

  /**
   * @fn 领取金币
   * @time 2024 5 29 15:16
   */
  btnGetPiggyBankGold() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    if (DatasSpace.Instance.piggyBankGlodNum < 300) {
      console.log('存钱罐未存满');
      Util.Instance.GloberLoadPrefab('Tips', 1, (res) => {
        let newNode = cc.instantiate(res);
        cc.find('Canvas').addChild(newNode);
        newNode.getComponent('Tips').setSting('存钱罐未满300');
      }, () => {

      });
    } else {
      if (DatasSpace.Instance.piggyBankGlodNum >= 300 && DatasSpace.Instance.piggyBankGlodNum < 600) {
        if (DatasSpace.Instance.piggyBankGold_tick300 == 1) {
          console.log('未存到600金币!');
          Util.Instance.GloberLoadPrefab('Tips', 1, (res) => {
            let newNode = cc.instantiate(res);
            cc.find('Canvas').addChild(newNode);
            newNode.getComponent('Tips').setSting('未存到600金币!');
          }, () => {

          });

        } else {
          console.log('领取300');
          DatasSpace.Instance.piggyBankGold_tick300 = 1;
          DatasSpace.Instance.setPiggyBankTick300();
          DatasSpace.Instance.goldNum += 300;
          DatasSpace.Instance.setGoldNum();
        }

      } else if (DatasSpace.Instance.piggyBankGlodNum >= 600) {
        console.log('领取600');
        DatasSpace.Instance.piggyBankGlodNum = 0;
        DatasSpace.Instance.setPiggyBankNum();
        DatasSpace.Instance.piggyBankGold_tick300 = 0;
        DatasSpace.Instance.setPiggyBankTick300();
        DatasSpace.Instance.goldNum += 600;
        DatasSpace.Instance.setGoldNum();
      }
    }

  }

  /**
   * @fn 领取金币
   * @time 2024 5 30 16:20
   */
  btnGetPiggyBankGoldByVideoAd() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    if (DatasSpace.Instance.piggyBankGlodNum < 300) {
      console.log('存钱罐未存满');
      Util.Instance.GloberLoadPrefab('Tips', 1, (res) => {
        let newNode = cc.instantiate(res);
        cc.find('Canvas').addChild(newNode);
        newNode.getComponent('Tips').setSting('存钱罐未存满300');
      }, () => {

      });
    } else {
      if (DatasSpace.Instance.piggyBankGlodNum >= 300 && DatasSpace.Instance.piggyBankGlodNum < 600) {
        if (DatasSpace.Instance.piggyBankGold_tick300 == 1) {
          console.log('未存到600金币!');
          console.log('未存到600金币!');
          Util.Instance.GloberLoadPrefab('Tips', 1, (res) => {
            let newNode = cc.instantiate(res);
            cc.find('Canvas').addChild(newNode);
            newNode.getComponent('Tips').setSting('未存到600金币!');
          }, () => {

          });
        } else {
          console.log('领取300');
          AdComponent.Instance.loadVideoAd(0, (res) => {
            if (res) {
              console.log('广告购买成功');
              DatasSpace.Instance.piggyBankGold_tick300 = 1;
              DatasSpace.Instance.setPiggyBankTick300();
              DatasSpace.Instance.goldNum += 600;
              DatasSpace.Instance.setGoldNum();
            }
          });

        }

      } else if (DatasSpace.Instance.piggyBankGlodNum >= 600) {
        console.log('领取600');

        AdComponent.Instance.loadVideoAd(0, (res) => {
          if (res) {
            console.log('广告购买成功');
            DatasSpace.Instance.piggyBankGlodNum = 0;
            DatasSpace.Instance.setPiggyBankNum();
            DatasSpace.Instance.piggyBankGold_tick300 = 0;
            DatasSpace.Instance.setPiggyBankTick300();
            DatasSpace.Instance.goldNum += 1200;
            DatasSpace.Instance.setGoldNum();
          }
        });


      }
    }

  }







}


